package boot.spring.config;

import boot.spring.po.Mail;
import boot.spring.service.impl.PublisherImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.UUID;

/**
 * 拦截所有的异常
 */

@ControllerAdvice
public class GlobalExceptionHandler {


    @Autowired
    PublisherImpl publisher;
    // 拦截所有异常类型
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        // 这里可以记录日志，处理特定的异常信息等
        Mail mail =new Mail(UUID.randomUUID().toString(),ex.toString(),0.44);
        publisher.sendfanoutMail(mail);
        return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
